﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Scavenger
{
    class Character
    {
        /* Class Description
         * A class for all the charcters in the game. Initializes with a buttload of stuff, that
         * probably are subject to change, as the class isn't fully defined yet.
         * 
         * TODO:
         *  - Update method needs to able to handle an AI-player.
         */

        #region Delcarations
        
        public Vector2 Position;
        public Animation Graphic;
        public bool Active;
        public float Rotation;
        public int Width
        {
            get { return Graphic.FrameWidth; }
        }
        public int Height
        {
            get { return Graphic.FrameHeight; }
        }
        public int Health, Exp; // Energy/mana?

        float moveSpeed;

        /* Additional classes to be created:
         *  - Backpack
         *  - Space suit
         *  - Class (Team Leader, Security Expert, Technician, Doc, Enemies)
         *  - Active/holstered weapon
         */
        #endregion
        
        public void Initialize(Vector2 position, Animation graphic, int health, int exp)
        {
            this.Position = position;
            this.Graphic = graphic;
            this.Health = health;
            this.Exp = exp;

            Rotation = 0;
            Active = true;

            // To be determined by backpack and equipment weight
            moveSpeed = 8.0f;
        }

        /// <summary>
        /// Calculates a characters current status and position, depending on mouse and keyboard state, as well as gametime. 
        /// </summary>
        /// <param name="gameTime">Current gametime</param>
        /// <param name="keyboardState">Current keyboard state</param>
        /// <param name="mouseState">Current mouse state</param>
        public void Update(GameTime gameTime, KeyboardState keyboardState, Pointer pointer)
        {
            // Move character
            // Should probably be adapted so that AI can manouver
            if (keyboardState.IsKeyDown(Keys.A))
                Position.X -= moveSpeed;
            if (keyboardState.IsKeyDown(Keys.D))
                Position.X += moveSpeed;
            if (keyboardState.IsKeyDown(Keys.W))
                Position.Y -= moveSpeed;
            if (keyboardState.IsKeyDown(Keys.S))
                Position.Y += moveSpeed;
            
            // Rotate character
            Rotation = (float)(Math.Atan((Position.Y - Height / 2 - pointer.Position.Y - pointer.Height / 2) / 
                (Position.X - Width / 2 - pointer.Position.X - pointer.Width / 2)) 
                + MathHelper.PiOver2);
            if ((Position.X - Width / 2) >= (pointer.Position.X - pointer.Width / 2))
                Rotation += MathHelper.Pi;
            

            //Graphic.Position = Position; // Added in Game1.cs
            //Graphic.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Graphic.Draw(spriteBatch, Rotation);
        }



    }
}
